Once you have created a sprite world, you can create sprites within it. To do this, you must first obtain image descriptions and image data for your sprites. This image data may be any image data that has been compressed using QuickTime's Image Compression Manager.
You create sprites and add them to your sprite world using the NewSprite function. If you want to create a sprite that is drawn to the background graphics world, you should specify the constant kBackgroundSpriteLayerNum for the layer parameter.
The sample code function CreateSprites , shown in Listing 2 , creates the sprites for the sample application. First, the function initializes some global arrays with position and image information for the sprites.
Next, CreateSprites iterates through all the sprite images, preparing each image for display. For each image, CreateSprites calls the sample code function MakePictTransparent function, which strips any surrounding background color from the image. MakePictTransparent does this by using the animation compressor to recompress the PICT images using a key color. Then, CreateSprites calls ExtractCompressData , which extracts the compressed data from the PICT image. This is one technique for creating compressed images; there are other, more optimized ways to store and retrieve sprite images.
Once the images have been prepared, CreateSprites calls NewSprite to create each sprite in the sprite world. CreateSprites creates each sprite in a different layer.
// constants
#define kNumSprites 4
#define kNumSpaceShipImages 24
#define kBackgroundPictID 158
#define kFirstSpaceShipPictID (kBackgroundPictID + 1)
#define kSpaceShipWidth 106
#define kSpaceShipHeight 80
// global variables
SpriteWorld gSpriteWorld = nil;
Sprite gSprites[kNumSprites];
Rect gDestRects[kNumSprites];
Point gDeltas[kNumSprites];
short gCurrentImages[kNumSprites];
Handle gCompressedPictures[kNumSpaceShipImages];
ImageDescriptionHandle gImageDescriptions[kNumSpaceShipImages];
void CreateSprites (void)
{
long i;
Handle compressedData = nil;
PicHandle picture;
CGrafPtr savePort;
GDHandle saveGD;
OSErr err;
RGBColor keyColor;
SetRect (&gDestRects[0], 132, 132, 132 + kSpaceShipWidth,
132 + kSpaceShipHeight);
SetRect (&gDestRects[1], 50, 50, 50 + kSpaceShipWidth,
50 + kSpaceShipHeight);
SetRect (&gDestRects[2], 100, 100, 100 + kSpaceShipWidth,
100 + kSpaceShipHeight);
SetRect (&gDestRects[3], 130, 130, 130 + kSpaceShipWidth,
130 + kSpaceShipHeight);
gDeltas[0].h = -3;
gDeltas[0].v = 0;
gDeltas[1].h = -5;
gDeltas[1].v = 3;
gDeltas[2].h = 4;
gDeltas[2].v = -6;
gDeltas[3].h = 6;
gDeltas[3].v = 4;
gCurrentImages[0] = 0;
gCurrentImages[1] = kNumSpaceShipImages / 4;
gCurrentImages[2] = kNumSpaceShipImages / 2;
gCurrentImages[3] = kNumSpaceShipImages * 4 / 3;
keyColor.red = keyColor.green = keyColor.blue = 0xFFFF;
// recompress PICT images to make them transparent
for (i = 0; i < kNumSpaceShipImages; i++)
{
picture = (PicHandle) GetPicture (i + kFirstSpaceShipPictID);
DetachResource ((Handle)picture);
MakePictTransparent (picture, &keyColor);
ExtractCompressData (picture, &gCompressedPictures[i],
&gImageDescriptions[i]);
HLock (gCompressedPictures[i]);
KillPicture (picture);
}
// create the sprites for the sprite world
for (i = 0; i < kNumSprites; i++)
{
MatrixRecord matrix;
SetIdentityMatrix (&matrix);
matrix.matrix[2][0] = ((long)gDestRects[i].left << 16);
matrix.matrix[2][1] = ((long)gDestRects[i].top << 16);
err = NewSprite (&(gSprites[i]), gSpriteWorld,
gImageDescriptions[i],* gCompressedPictures[i],
&matrix, true, i);
}
}